﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FUI;
using System.IO;
using System.Text.RegularExpressions;
using CUILoader;
using FUINetCF;
using System.Xml;

namespace ComboBoxFUI
{
    class CComboBoxFUI :FUI.FUIBase
    {
        public CComboBoxFUI()
        {
            StrName = "NetCFComboBox";
            loadXMLProperties();
        }
      
        public override string generateDeclaration()
        {
            FileStream file;
            file = new FileStream(StrPath + "\\NetCFComboBoxDeclaration.txt", FileMode.OpenOrCreate, FileAccess.Read);
            StreamReader sr = new StreamReader(file);
            string s = sr.ReadToEnd();
            sr.Close();
            file.Close();
            string pattern = "###ComboBoxName###";
            string replacement = generateDataType(this.getAttributeFromName("Name"));
            Regex rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);
            return s;
        }

        public override string generateInitialization()
        {
            FileStream file;
            file = new FileStream(StrPath + "\\NetCFComboBoxInitialization.txt", FileMode.OpenOrCreate, FileAccess.Read);
            StreamReader sr = new StreamReader(file);
            string s = sr.ReadToEnd();
            sr.Close();
            file.Close();

            //Su dung regex de thay cac pattern trong Initialization.txt

            //Add them các Items
            CAttribute attItemCollection = new CAttribute();
            foreach (CAttribute att in LstAttributes)
            {
                if (att.StrName == "ItemCollection")
                {
                    attItemCollection = att;
                    break;
                }
            }
            if (attItemCollection.StrValue != "")
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(attItemCollection.StrValue);

                XmlNodeList nodes = xmlDoc.GetElementsByTagName("Item");
                foreach (XmlNode n in nodes)
                {
                    //this.###ComboBoxName###.Items.Add("###ItemName###");
                    s += "\n\tthis.###ComboBoxName###.Items.Add(\"";
                    s += n.Attributes["ItemText"].Value;
                    s += "\");";
                }
            }

            string pattern = "###ComboBoxName###";
            string replacement = generateDataType(this.getAttributeFromName("Name"));
            Regex rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###IsEnable###";
            replacement = generateDataType(this.getAttributeFromName("IsEnable"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###FontSize###";
            replacement = generateDataType(this.getAttributeFromName("FontSize"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###FontStyle###";
            replacement = generateDataType(this.getAttributeFromName("FontStyle"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###ForeColor###";
            replacement = generateDataType(this.getAttributeFromName("ForeColor"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###XLocation###";
            replacement = generateDataType(this.getAttributeFromName("XLocation"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###YLocation###";
            replacement = generateDataType(this.getAttributeFromName("YLocation"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###Name###";
            replacement = generateDataType(this.getAttributeFromName("Name"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###WSize###";
            replacement = generateDataType(this.getAttributeFromName("WSize"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###HSize###";
            replacement = generateDataType(this.getAttributeFromName("HSize"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###Text###";
            replacement = generateDataType(this.getAttributeFromName("Text"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###IsVisible###;";
            replacement = generateDataType(this.getAttributeFromName("IsVisible"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);            

            return s;
        }

        public override string generateDataType(CAttribute m_Att)
        {
            CNetCFConverter m_NetCFConverter = new CNetCFConverter();
            return m_NetCFConverter.NetCFGenerateDataType(m_Att);
        }
    }
}
